package cn.skyclass.net
{
	import cn.skyclass.beans.FlexBean;
	import cn.skyclass.utils.Md5Digest;
	
	/**
	 * 实现简单的用户 token 模型，完成用户的身份验证问题
	 */
	[Bindable]
	[RemoteClass(alias="cn.skyclass.net.UserToken")]	 
	public class UserToken extends FlexBean
	{
		public function generateToken():void {
			// 自定义生成 token 的算法...

			this.randomNum = Math.random().toString();
			this.clientIp = URLUtilEx.getClientIp();
			
			var temp:String = Md5Digest.md5(this.userId + this.userName);
			temp = Md5Digest.md5(temp + this.encryptedPassword + this.clientIp + this.randomNum + "user-token");
			this.token = temp;
		}	
		
		private static var emptyToken:UserToken;
		public static function getEmpty():UserToken{
			if(!emptyToken){
				var t:UserToken = new UserToken();
				t.userId = 0;
				t.userName = "empty";
				t.userNumber = "empty";
				t.encryptedPassword = "empty";
				t.clientIp = URLUtilEx.getClientIp();
				t.generateToken();
				emptyToken = t;
			}
			return emptyToken;				
		}
			
		override public function toString():String{
			var join:String = "&&";
			var temp:String = "";
			temp += this.userId + join + this.userName + join + this.userNumber;
			temp += join + this.encryptedPassword + join + this.clientIp;
			temp += join + this.randomNum + join + this.token;
			return temp;			
		}
		
		public var userId:int;
		public var userName:String;
		public var userNumber:String;
		
		public var encryptedPassword:String;
		public var clientIp:String;
		public var randomNum:String;
		public var logined:Boolean;	
		
		public var token:String;	
	}
}